<?php
/**
 * Created by PhpStorm.
 * User: johnwang
 * Date: 2020/4/24
 * Time: 上午10:19
 */

namespace app\api\service;


use think\Log;

/**
 * Class
 * @package app\api\service
 *
 * 美葩传媒
 */
class VbillService
{

    protected $privateKey = "MIIEpQIBAAKCAQEA09rgFPcsHGMkMDrYqi3rwq/c3FJDgaDFNVhEtX6jwEjGb+x0
uqz8fjeg0nZ6qClvb/NBqS5Lmz+k7uXZ0mYL98kaDc9sz82UY5dENikP8nvAj7rx
Bq1fsRRieQXjNa/pH4WKgtcFhZsiGJSOge6klVpMwLrbUShqSHF76ihR+aJiISio
6Xx3Ok9NkHHYb0EwB6lzYCRZ7qldgNsNaOEWJTC4z1pe63yEankPUNH90Du7QvQl
KzRJaOdnRmPWfNaTZT+K+JA4soZ1+jVMlPPshvkr2CW7+q0Ar94wkIVDvSSDsEl7
GAvLbjillIYr0dYKi0aBiFSxmkZn0lVQUv0HwwIDAQABAoIBAAEFgScYugG5CKei
4QCREB30MyBs1R5+4/SJ5GrrnWaD81D+1uE/jEgfhIr/P0AoUv07MwnZy0uIVwc/
3HkDPPOI6LNAWMn3t8tzI0UPlaAZsKgAwsDBfUChVIg42wubOYiJYE2ZM9GdGeD8
dmNx/yEpOk2DmSMfCsQmp+5NG3gUqh04DfEGj7LPPWZyPwhU6nY4jKq6I/vFTeOU
uFlfPJFlWQ48oQj8r+ef9wm2kbYopA+kzHE+yVrQaVo0Ij/EECfzssTIh1y1wLRH
LzoRmgMR4dDbGgD8EZOsB2PmkV/3pDFNAE5U8M1lsRAofxbvxHLiLYSsrM+PtC6v
W2k3rkECgYEA8LL0Ph8PGiUYuSwX+xLJv1n2R1UtKIFwuB01zdM6TjHVtelmteze
XFVQYI9OQQZWy4aGqXlKLuX01lQkIYqxwiw4Pl085aaDgbsrRwHQJhPWasM5/79M
Gl8T8ZPSnGjxawzHJ2RIIqOmuTFWhhdlmI4UwsgC4pX64Iw27/YZ3zsCgYEA4VKG
HCTBB7gZPsXaSXMxIF18/AVzYHxhbZVhQSFUMl/7DPTKH5WGwCI59+2UZ/Idd5tT
fUeaOZ6uYXXn2Kw4UtSw147uwDh5tQgKbEF0uPGZ73iiIJ4nvGYsOJAswnCKddmh
biqnMj01Mi4TF5r1zdDwnAkTCSeGEK4wHxZVARkCgYEAgsr7ZXZDIpXd5WFMdqYy
8sPTy9dA/q23rpRQ1FNfb3YtBviZU2ovOkUxtAarIzJqn5WuPxElem6CqKR4LbHL
/hNBTMIlgTUGaMxoK+GUVdyZutHzBkHQ/NOFZtRIsa1k4iLDZWhculXrFt70gVDd
aBRsu5sr1ixhcvs+Lw6v6FkCgYEA0P+P38kDt1o/KGezGxAbEE9HICkaJPQaFuBJ
bnUwuO0oW5MvbG5wqATXSi33yxbb2Fad8YWZ+zpBu6r225abQ9F/GuK8eFWqJo4H
xPROBUIsq+B0Wdb5Opp3CBxLOCvVcozkXOB5Z3M4xYvRL8MPntHgbItKOeKMSyqW
/i7jt/kCgYEAvAM4VtehMiJrNcGyMjt4hJ4Lx9/ae4D/L0tDK9KPgw7dkQb8ZDXq
irK1A4HIEsCwNSpcNuE4L50HhSIsczQ7jG+plYf/QkZhFXHSkywbVBZAyslAj5LS
mAsIU4lMyaytto6XTn9ypK48r9DNcU5vh0oFEBJeqwVOCJulTNqYXQU=";
    //随行付公钥
    protected $sxfPublic =
        "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCOmsrFtFPTnEzfpJ/hDl5RODBxw4i9Ex3NmmG/N7A1+by032zZZgLLpdNh8y5otjFY0E37Nyr4FGKFRSSuDiTk8vfx3pv6ImS1Rxjjg4qdVHIfqhCeB0Z2ZPuBD3Gbj8hHFEtXZq8+msAFu/5ZQjiVhgs5WWBjh54LYWSum+d9+wIDAQAB";

    protected $url = "https://openapi.tianquetech.com";
    protected $testUrl = "https://openapi-test.tianquetech.com";

    protected $jsapiScan = "/order/jsapiScan";
    protected $refund = "/order/refund";
    protected $commitApply = "/merchant/realName/commitApply";
    protected $orgId = "92780514";

    protected $uploadUrl = "/merchant/uploadPicture";//上传图片
    protected $merchantUrl = "/merchant/income";//进件
    protected $queryMerchantInfo = "/merchant/queryMerchantInfo";
    protected $editMerchantInfo = "/merchant/editMerchantInfo";//编辑
    protected $updateMerchantInfo = "/merchant/updateMerchantInfo";//修改商户信息
    protected $weixinVerify = "/merchant/realName/commitApply";//微信认证
    protected $queryApplyInfo = "/merchant/realName/queryApplyInfo";//微信实名认证查询
    protected $queryBalance = "/capital/query/queryBalance";//查询余额
    protected $reverseScan = "/order/reverseScan";
    protected $querySettlement = "/capital/query/querySettlement";//结算查询
    protected $poundageQuery = "/query/poundageQuery";//手续费查询
    protected $queryGrantStatus = "/merchant/realName/queryGrantStatus";//微信授权查询

    protected $merchantInfoQuery = "/merchant/merchantInfoQuery";//商户信息查询
    protected $getFileUrl = "/capital/fileDownload/getFileUrl";//对账文件

    protected $appletScanPre = "/order/appletScanPre";


    public function __construct()
    {

        import('.sxf.AopClient', EXTEND_PATH, '.php');

    }

    /**
     * 聚合支付
     */
    public function jsApi($req)
    {


//        $req = array();
//        $req['mno'] = $param['mno'];
//        $req['ordNo'] = $param['ordNo'];
//        $req['amt'] = $param['amt'];
//        $req['payType'] = $param['payType'];
//        $req['payWay'] = $param['payWay'];
//        $req['subject'] = $param['subject'];
//        $req['tradeSource'] = $param['tradeSource'];
//        $req['trmIp'] = $param['trmIp'];
//        $req['subAppid'] = $param['subAppid'];
//        $req['notifyUrl'] = $param['notifyUrl'];

        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $this->getRandom(32),
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );

        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);

        $reqStr = json_encode($reqStr, 320);
        Log::info("requestPay==2=".$reqStr);

        $url = $this->url . $this->jsapiScan;
        $resp = $aopClient->curl($url, $reqStr);

        return $resp;
    }


    public function pay($req)
    {


//        $req = array();
//        $req['mno'] = $param['mno'];
//        $req['ordNo'] = $param['ordNo'];
//        $req['amt'] = $param['amt'];
//        $req['payType'] = $param['payType'];
//        $req['payWay'] = $param['payWay'];
//        $req['subject'] = $param['subject'];
//        $req['tradeSource'] = $param['tradeSource'];
//        $req['trmIp'] = $param['trmIp'];
//        $req['subAppid'] = $param['subAppid'];
//        $req['notifyUrl'] = $param['notifyUrl'];

        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $this->getRandom(32),
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );

        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);

        $reqStr = json_encode($reqStr, 320);
        Log::info("requestPay==2=".$reqStr);

        $url = $this->url . $this->appletScanPre;
        $resp = $aopClient->curl($url, $reqStr);

        return $resp;
    }


    /**
     * 主扫
     */
    public function scanPay($req = array())
    {

        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $this->getRandom(32),
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );

        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);
        $url = $this->url . $this->reverseScan;
        $resp = $aopClient->curl($url, $reqStr);

        return $resp;

    }


   public function  getRandom($param){
        $str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
        $key  =  "" ;
        for ( $i =0; $i < $param ; $i ++)
        {
            $key  .=  $str [mt_rand(0,32)];     //生成php随机数
        }
        return  $key;
    }

    /**
     * @param array $param
     * @throws \Exception
     * 退款
     */
    public function refund($req)
    {

        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $this->getRandom(32),
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );

        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);
        $url = $this->url . $this->refund;
        $resp = $aopClient->curl($url, $reqStr);

        return $resp;
    }

    /**
     * 微信实名认证
     */
    public function weixinVerify($req)
    {


        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $this->getRandom(32),
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );

        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);
        $url = $this->url . $this->commitApply;
        $resp = $aopClient->curl($url, $reqStr);

        return $resp;

    }

    /**
     * 入网
     */
    public function merchant($req, $isUpdate)
    {

        $reqId = $this->getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);
        Log::info($reqStr);

        if ($isUpdate == 1) {
            $requestUrl = $this->url . $this->merchantUrl;
        } else {
            $requestUrl = $this->url . $this->updateMerchantInfo;

        }
        $resp = $aopClient->curl($requestUrl, $reqStr);

        return $resp;
    }

    /**
     * 上传图片
     */
    public function upload($req)
    {

        /**
         * 3. 上传随行付图片服务器
         */
        $post_data = array(
            "file" => new \CURLFile($req['filePath']),
            'orgId' => $this->orgId,  //文件类型
            'pictureType' => $req['type'],
            'reqId' => $this->getRandom(32),
        );
        $ch = curl_init();
        //  设置变量
        curl_setopt($ch, CURLOPT_URL, $this->url . $this->uploadUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//执行结果是否被返回，0是返回，1是不返回
        curl_setopt($ch, CURLOPT_HEADER, 0);//参数设置，是否显示头部信息，1为显示，0为不显示
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 100);//设置curl执行超时时间最大是多少
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        //   执行并获取结果
        $sxfRet = curl_exec($ch);//运行curl
        curl_close($ch);

        return $sxfRet;

    }

    /**
     * 入驻结果查询
     */
    public function queryMerchant($req)
    {


        $reqId = $this->getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->queryMerchantInfo;

        $resp = $aopClient->curl($requestUrl, $reqStr);

        return $resp;
    }

    /**
     * 微信实名认证查询
     */

    public function queryWeixinVerify($req)
    {


        $reqId = $this->getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->queryApplyInfo;

        error_log("====query wechat verify===" . $reqStr);
        $resp = $aopClient->curl($requestUrl, $reqStr);
        return $resp;
    }

    public function settle($req)
    {
        $reqId = $this->getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );
        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->querySettlement;

        $resp = $aopClient->curl($requestUrl, $reqStr);
        return $resp;
    }


    /**
     *
     * 查询余额
     *
     */

    public function balance($req)
    {


        $reqId = $this->getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->queryBalance;

        $resp = $aopClient->curl($requestUrl, $reqStr);
        return $resp;
    }

    /**
     * 手续费查询
     */
    public function feeQuery($req)
    {


        $reqId = $this->getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->poundageQuery;

        $resp = $aopClient->curl($requestUrl, $reqStr);
        return $resp;
    }

    public function queryStatus($req)
    {

        $reqId =$this->getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->queryGrantStatus;

        $resp = $aopClient->curl($requestUrl, $reqStr);
        return $resp;
    }

    /**
     * 对账文件下载
     */
    public function getFileUrl($req){
        $reqId = $this->getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );
        error_log("====getFile==".json_encode($reqJson));


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->getFileUrl;

        $resp = $aopClient->curl($requestUrl, $reqStr);
        return $resp;

    }


    /**
     * 商户信息查询
     */
    public function merchatQuery($req)
    {

        $reqId = $this->getRandom(32);
        $reqJson = array(
            "reqData" => $req,
            "orgId" => $this->orgId,
            "reqId" => $reqId,
            "version" => "1.0",//OEM和代理商的要传2.0，服务商传1.0
            "signType" => "RSA",
            "timestamp" => date('Ymdhis', time()),
        );


        $aopClient = new \AopClient();
        $signContent = $aopClient->generateSign($reqJson, $this->privateKey);
        $sign = array(
            "sign" => $signContent
        );

        $reqStr = array_merge($reqJson, $sign);
        $reqStr = json_encode($reqStr, 320);

        $requestUrl = $this->url . $this->merchantInfoQuery;

        $resp = $aopClient->curl($requestUrl, $reqStr);
        return $resp;
    }

}